package com.caidan.search.controller;

import cn.hutool.core.util.StrUtil;
import com.caidan.common.result.Result;
import com.caidan.common.result.ResultCodeEnum;
import com.caidan.common.util.AuthContextHolder;
import com.caidan.common.util.PhoneNumberValidator;
import com.caidan.model.domain.user.User;
import com.caidan.model.dto.user.UserLoginDTO;
import com.caidan.model.dto.user.UserRegisterDTO;
import com.caidan.model.dto.user.UserUpdateDTO;
import com.caidan.model.vo.user.UserVo;
import com.caidan.search.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

/**
 * @项目名 CaiDanSearchSystem
 * @描述
 * @作者 小菜蛋
 * @时间 2024/12/1 18:49
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("login")
    private Result<String> userLogin(@RequestBody UserLoginDTO userLoginDTO) {
//        使用MD5?
        if (null == userLoginDTO || null == userLoginDTO.getPhoneNumber()) {
            log.error("用户登录参数传递为空");
            return Result.fail(ResultCodeEnum.DATA_ERROR.getMessage());
        }
        if(!PhoneNumberValidator.isValidPhoneNumber(userLoginDTO.getPhoneNumber())){
            return Result.fail(ResultCodeEnum.PHONE_ERROR.getMessage());
        }
        return userService.userLogin(userLoginDTO);
    }

    @PostMapping("/register")
    private Result<String> userRegister(@RequestBody UserRegisterDTO userRegisterDTO) {
        if (null == userRegisterDTO || null == userRegisterDTO.getCode() || null == userRegisterDTO.getPhoneNumber()) {
            return Result.fail(ResultCodeEnum.ARGUMENT_VALID_ERROR.getMessage());
        }
        if(!PhoneNumberValidator.isValidPhoneNumber(userRegisterDTO.getPhoneNumber())){
            return Result.fail(ResultCodeEnum.PHONE_ERROR.getMessage());
        }
        return userService.userRegister(userRegisterDTO);
    }

    @GetMapping("/info")
    public Result<UserVo> getUserVoMessage(HttpServletResponse httpServletResponse) {
        Long id = AuthContextHolder.getUserId();
        if (null == id || id <= 0) {
            httpServletResponse.setStatus(401);
            return Result.fail();
        }
        return userService.getUserVoMessage(id);
    }


    @PutMapping("/update")
    public Result updateUserMessage(@RequestBody UserUpdateDTO userUpdateDTO){
        Long userId = AuthContextHolder.getUserId();

        if(null == userUpdateDTO || null == userId){
            return Result.fail(ResultCodeEnum.ARGUMENT_VALID_ERROR.getMessage());
        }
        return userService.updateUserMessage(userId,userUpdateDTO);
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteUserMessage(@PathVariable Long id){
        if (null == id || id <= 0) {
            return Result.fail(ResultCodeEnum.ARGUMENT_VALID_ERROR.getMessage());
        }
        return userService.deleteUserMessage(id);
    }

    @GetMapping("/system")
    public Result updateSystemImg(@RequestParam("url") String url){
        if(StrUtil.isBlank(url)){
            return Result.fail(Result.fail(ResultCodeEnum.ARGUMENT_VALID_ERROR.getMessage()));
        }
        return userService.updateSystemImg(url);
    }

}
